package com.Tian.service.Impl;

import com.Tian.dao.UserDetailMapper;
import com.Tian.dao.UserInfoMapper;
import com.Tian.dao.UserRoleRelationMapper;
import com.Tian.model.UserDetail;
import com.Tian.model.UserInfo;
import com.Tian.model.UserInfoParams;
import com.Tian.model.UserRoleRelation;
import com.Tian.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class ServiceImpl implements UserService {
    @Autowired(required = false)
    UserInfoMapper userInfoMapper;
    @Autowired(required = false)
    UserDetailMapper userDetailMapper;
    @Autowired(required = false)
    UserRoleRelationMapper userRoleRelationMapper;

    @Override
    public List<UserInfo> get() {
        List<UserInfo> userInfos= userInfoMapper.query();
        return userInfos;
    }

    @Override
    public HashMap<String, Object> select(UserInfo userInfo) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //1 设置分页的参数
        PageHelper.startPage(userInfo.getPage(),userInfo.getRow());
        //2 查询自定义分页的sql
        List<UserInfo> userInfoList = userInfoMapper.query();
        //3 把集合转换成分页对象
        PageInfo<UserInfo> pageInfo = new PageInfo<>(userInfoList);
        //当前页的集合
        map.put("userList",pageInfo.getList());
        //数据总条数
        map.put("total",pageInfo.getTotal());
        //页数
        map.put("totalPage",pageInfo.getPages());
        //上一页
        if (pageInfo.getPrePage()==0){
            map.put("prePage",pageInfo.getPrePage()+1);
        }else {
            map.put("prePage",pageInfo.getPrePage());
        }
        //下一页
        if((pageInfo.getPrePage()+2)>=pageInfo.getPages()){
            map.put("nextPage",pageInfo.getPages());
        }else{
            map.put("nextPage",pageInfo.getPrePage()+2);
        }

        //当前页
        map.put("curPage",userInfo.getPage());
        return map;
    }

    //实习级联新增功能
    @Override
    public HashMap<String, Object> addRole(UserInfoParams userInfoParams) {
        HashMap<String,Object> map=new HashMap<String,Object>();
        //新增userInfo
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userInfoParams.getUserName());
        userInfo.setUserPwd(userInfoParams.getUserPwd());
        int num = userInfoMapper.insertSelective(userInfo);
        if (num>0){
            //新增userDetail
            UserDetail userDetail = new UserDetail();
            userDetail.setEmail(userInfoParams.getEmail());
            userDetail.setPhone(userInfoParams.getPhone());
            userDetail.setUrl(userInfoParams.getUrl());
            UserInfo userInfo1 = userInfoMapper.selectUserName(userInfoParams.getUserName());
            //设置默认为0
            userDetail.setState(0);
            userDetail.setUId(userInfo1.getUId());
            int sum = userDetailMapper.insertSelective(userDetail);
            if (sum>0){
               //新增用户和角色中间表,通过判断用户长度
                if (userInfoParams.getRoleId().length()>1){
                    //数组
                  String[] arr= userInfoParams.getRoleId().split(",");
                    List<UserRoleRelation> list = new ArrayList<>();
                  for (int i= 0; i<arr.length;i++){
                      UserRoleRelation userRoleRelation = new UserRoleRelation();
                      userRoleRelation.setUId(userInfo1.getUId());
                      userRoleRelation.setRId(Integer.parseInt(arr[i]));
                      list.add(userRoleRelation);
                  }
                int dd=  userRoleRelationMapper.insertBitch(list);
                  if (dd>0){
                      map.put("info","新增成功");
                      return map;
                  }
                }
            }
        }
        //错误提示
        map.put("info","新增失败");
        return map;
    }
    //实现的是级联修改
    @Override
    public UserInfo selectUserId(Integer uId) {
        return userInfoMapper.selectUserId(uId);
    }

    //将数据库的值展示到表单上
//    @Override
//    public HashMap<String, Object> select() {
//        List<UserInfo> list = userInfoMapper.query();
//        HashMap<String, Object> map = new HashMap<String, Object>();
//        map.put("userList",list);
//        return map;
//    }
}
